//
//  RootViewController.m
//  MyTreeViewPrototype
//
//  Created by Jon Limjap on 4/21/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "TreeView.h"
#import "MyTreeViewCell.h"
#import "KakaoLinkCenter.h"

@implementation TreeView
@synthesize Trdelegate;
@synthesize treeNode;
#pragma mark -
#pragma mark View lifecycle


- (void)viewDidLoad {
    [super viewDidLoad];

	self.treeNode = [[MyTreeNode alloc] initWithValue:@"Root"];
	MyTreeNode *node1 = [[MyTreeNode alloc] initWithValue:@"주택용 바닥재 - 마루"];
	[treeNode addChild:node1];
	
	MyTreeNode *node1a = [[MyTreeNode alloc] initWithValue:@"지아마루 - 프레스티지"];
	MyTreeNode *node1b = [[MyTreeNode alloc] initWithValue:@"지아마루 - 7"];
    MyTreeNode *node1c = [[MyTreeNode alloc] initWithValue:@"원목마루"];
    MyTreeNode *node1d = [[MyTreeNode alloc] initWithValue:@"녹차마루"];
    MyTreeNode *node1e = [[MyTreeNode alloc] initWithValue:@"강그린마루"];
    MyTreeNode *node1f = [[MyTreeNode alloc] initWithValue:@"포르테마루 - 광폭"];
    MyTreeNode *node1g = [[MyTreeNode alloc] initWithValue:@"포르테마루 - 소폭"];
	[node1 addChild:node1a];
	[node1 addChild:node1b];
    [node1 addChild:node1c];
    [node1 addChild:node1d];
    [node1 addChild:node1e];
    [node1 addChild:node1f];
    [node1 addChild:node1g];
    
    MyTreeNode *node2 = [[MyTreeNode alloc] initWithValue:@"주택용 바닥재 - PVC시트"];
	[treeNode addChild:node2];
	
	MyTreeNode *node2a = [[MyTreeNode alloc] initWithValue:@"소리잠"];
	MyTreeNode *node2b = [[MyTreeNode alloc] initWithValue:@"휴앤미"];
    MyTreeNode *node2c = [[MyTreeNode alloc] initWithValue:@"네이쳐라이프"];
    MyTreeNode *node2d = [[MyTreeNode alloc] initWithValue:@"자연애2.5"];
    MyTreeNode *node2e = [[MyTreeNode alloc] initWithValue:@"자연애2.2"];
    MyTreeNode *node2f = [[MyTreeNode alloc] initWithValue:@"자연애1.8"];
    MyTreeNode *node2g = [[MyTreeNode alloc] initWithValue:@"뉴청맥1.8"];

	[node2 addChild:node2a];
	[node2 addChild:node2b];
    [node2 addChild:node2c];
    [node2 addChild:node2d];
    [node2 addChild:node2e];
    [node2 addChild:node2f];
    [node2 addChild:node2g];
    
    MyTreeNode *node3 = [[MyTreeNode alloc] initWithValue:@"주택용 바닥재 - PVC타일"];
	[treeNode addChild:node3];
    MyTreeNode *node3a = [[MyTreeNode alloc] initWithValue:@"하우스"];
	[node3 addChild:node3a];

	node1.inclusive = NO;
    node2.inclusive = NO;
    node3.inclusive = NO;
//	node1b.inclusive = NO;
    
	MyTreeNode *node4 = [[MyTreeNode alloc] initWithValue:@"상업용 바닥재 - PVC타일"];
	[treeNode addChild:node4];
    node4.inclusive = NO;
	
	MyTreeNode *node4a = [[MyTreeNode alloc] initWithValue:@"데코타일 파인플러스"];
    MyTreeNode *node4b = [[MyTreeNode alloc] initWithValue:@"데코타일 에코노"];
    MyTreeNode *node4c = [[MyTreeNode alloc] initWithValue:@"하우스 이지"];
    MyTreeNode *node4d = [[MyTreeNode alloc] initWithValue:@"VIP타일 인레이드"];
    MyTreeNode *node4e = [[MyTreeNode alloc] initWithValue:@"VIP타일 마블/포스트"];
    MyTreeNode *node4f = [[MyTreeNode alloc] initWithValue:@"전도성타일"];
    MyTreeNode *node4g = [[MyTreeNode alloc] initWithValue:@"VCT타일-갤런트/GR"];
    MyTreeNode *node4h = [[MyTreeNode alloc] initWithValue:@"디럭스타일"];
    MyTreeNode *node4i = [[MyTreeNode alloc] initWithValue:@"기능성타일-OA타일"];
	[node4 addChild:node4a];
    [node4 addChild:node4b];
    [node4 addChild:node4c];
    [node4 addChild:node4d];
    [node4 addChild:node4e];
    [node4 addChild:node4f];
    [node4 addChild:node4g];
    [node4 addChild:node4h];
    [node4 addChild:node4i];

    MyTreeNode *node5 = [[MyTreeNode alloc] initWithValue:@"상업용 바닥재 - PVC시트"];
	[treeNode addChild:node5];
    node5.inclusive = NO;
    
    MyTreeNode *node5a = [[MyTreeNode alloc] initWithValue:@"우븐"];
    MyTreeNode *node5b = [[MyTreeNode alloc] initWithValue:@"지아플로어 호모젠"];
    MyTreeNode *node5c = [[MyTreeNode alloc] initWithValue:@"엘스트롱 유나이트"];
    MyTreeNode *node5d = [[MyTreeNode alloc] initWithValue:@"엘스트롱 클레버"];
    MyTreeNode *node5e = [[MyTreeNode alloc] initWithValue:@"엘스트롱 노블아트"];
    MyTreeNode *node5f = [[MyTreeNode alloc] initWithValue:@"네이쳐라이프"];
    MyTreeNode *node5g = [[MyTreeNode alloc] initWithValue:@"와이드"];
    MyTreeNode *node5h = [[MyTreeNode alloc] initWithValue:@"EQ플로어"];
    MyTreeNode *node5i = [[MyTreeNode alloc] initWithValue:@"렉스코트"];
    MyTreeNode *node5j = [[MyTreeNode alloc] initWithValue:@"SD메탈/트랜스"];
	[node5 addChild:node5a];
    [node5 addChild:node5b];
    [node5 addChild:node5c];
    [node5 addChild:node5d];
    [node5 addChild:node5e];
    [node5 addChild:node5f];
    [node5 addChild:node5g];
    [node5 addChild:node5h];
    [node5 addChild:node5i];
    [node5 addChild:node5j];
    
    MyTreeNode *node6 = [[MyTreeNode alloc] initWithValue:@"상업용 바닥재 - 카펫타일"];
	[treeNode addChild:node6];
    node6.inclusive = NO;
    
    MyTreeNode *node6a = [[MyTreeNode alloc] initWithValue:@"Style"];
    MyTreeNode *node6b = [[MyTreeNode alloc] initWithValue:@"L1000"];
    MyTreeNode *node6c = [[MyTreeNode alloc] initWithValue:@"L9300"];
    MyTreeNode *node6d = [[MyTreeNode alloc] initWithValue:@"L9600"];
    MyTreeNode *node6e = [[MyTreeNode alloc] initWithValue:@"L9700"];
    MyTreeNode *node6f = [[MyTreeNode alloc] initWithValue:@"L3300"];
    MyTreeNode *node6g = [[MyTreeNode alloc] initWithValue:@"L7000"];

	[node6 addChild:node6a];
    [node6 addChild:node6b];
    [node6 addChild:node6c];
    [node6 addChild:node6d];
    [node6 addChild:node6e];
    [node6 addChild:node6f];
    [node6 addChild:node6g];
    
    MyTreeNode *node7 = [[MyTreeNode alloc] initWithValue:@"상업용 바닥재 - 스톤타일"];
	[treeNode addChild:node7];
    node7.inclusive = NO;
    
    MyTreeNode *node7a = [[MyTreeNode alloc] initWithValue:@"마블"];
    MyTreeNode *node7b = [[MyTreeNode alloc] initWithValue:@"폴리싱"];
    [node7 addChild:node7a];
    [node7 addChild:node7b];
    
    MyTreeNode *node9 = [[MyTreeNode alloc] initWithValue:@"MY샘플북"];
	[treeNode addChild:node9];
//        MyTreeNode *node8 = [[MyTreeNode alloc] initWithValue:@""];
//    	[treeNode addChild:node8];
    
//    MyTreeNode *node4 = [[MyTreeNode alloc] initWithValue:@"MY샘플북"];
//	[treeNode addChild:node4];
    
    
//	MyTreeNode *node2a1 = [[MyTreeNode alloc] initWithValue:@"Node2a1"];
//	[node2a addChild:node2a1];
    
    
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
}


#pragma mark -
#pragma mark Table view data source

// Customize the number of sections in the table view.
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    descendantCount = descendantCount +1;
    NSLog(@"%d",[treeNode descendantCount]);
//    self.contentSizeForViewInPopover = CGSizeMake(self.view.frame.size.width, [treeNode descendantCount]*44 + 100);
    if(section == 0)
    return [self.treeNode descendantCount];
    else return 0;
        
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"Cell";
	
   	MyTreeNode *node = [[treeNode flattenElements] objectAtIndex:indexPath.row + 1];
//	MyTreeViewCell *cell = [[MyTreeViewCell alloc] initWithStyle:UITableViewCellStyleDefault
//												 reuseIdentifier:CellIdentifier 
//														   level:[node levelDepth] - 1 
//														expanded:node.inclusive];   
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    if([node.value isEqualToString:@"포르테마루 - 소폭"])
    {
        if([node levelDepth] - 1 != 0)
        {
            UIImage* aaa = [UIImage imageNamed:@"bg_lnb_b"];
            UIImageView* aaaa = [[UIImageView alloc] initWithImage:aaa];
            aaaa.frame = CGRectMake(0, 0, 188, 61);
            [cell addSubview:aaaa];
        }
    }
    else 
    {
        UIImage* bbb = [UIImage imageNamed:@"bg_lnb_m"];
        UIImageView* bbbb = [[UIImageView alloc] initWithImage:bbb];
        bbbb.frame = CGRectMake(0, 0, 188, 50);
        [cell addSubview:bbbb];
       
    }
    

    if([node levelDepth] - 1 == 0)
    {
        UIFont *font;
        font = [UIFont boldSystemFontOfSize:14];
        UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, cell.frame.size.width, cell.frame.size.height)];
        newLabel.text = node.value;
        newLabel.font = font;
        newLabel.backgroundColor = [UIColor clearColor];
     [cell addSubview:newLabel];
    }
    else 
    {
        UIFont *font;
        font = [UIFont systemFontOfSize:12];
        UILabel *newLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 0, cell.frame.size.width, cell.frame.size.height)];
        newLabel.text = node.value;
        newLabel.font = font;
          newLabel.backgroundColor = [UIColor clearColor];
        [cell addSubview:newLabel];
    }
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setSelected:NO animated:NO];
//    [tableView reloadData];
    return cell;

}



#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	MyTreeNode *node = [[treeNode flattenElements] objectAtIndex:indexPath.row + 1];
    
	if (!node.hasChildren)
    {
        [self.Trdelegate TreeViewSelect:indexPath.row haschilden:YES];
        [tableView reloadData];
//        UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
//        [cell setSelected:NO animated:NO];

        return;
    }
    else 
    {
        node.inclusive = !node.inclusive;	
        [treeNode flattenElementsWithCacheRefresh:YES];
        [self.Trdelegate TreeViewSelect:indexPath.row haschilden:NO];
        [tableView reloadData];
//        UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath];
//        [cell setSelected:NO animated:NO];

    }
    
    
    /////
//    
//    NSString *message = @"카카오링크를 사용하여 메시지를 전달해보세요."; // 메세지 
//    NSString *referenceURLString = @"http://link.kakao.com"; // 링크 넘기기
//    NSString *appBundleID = @"com.example.app";
//    NSString *appVersion = @"2.0";
//    NSString *appName = @"example"; // 제목
//    
//    if ([[KakaoLinkCenter defaultCenter] canOpenKakaoLink]) {
//        [[KakaoLinkCenter defaultCenter] openKakaoLinkWithURL:referenceURLString 
//                                                   appVersion:appVersion
//                                                  appBundleID:appBundleID
//                                                      appName:appName
//                                                      message:message];
//    } else {
//        // 카카오톡이 설치되어 있지 않은 경우에 대한 처리
//        NSLog(@"카카오톡 없음요");
//    }
    
    
//    NSLog(@"%d",indexPath.row);
    
	}


#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end

